Imports System.Data.SqlClient
Imports DevExpress.XtraEditors

'--- Created By : Priyabrata
'--- Created On : 27-Sep-2010

Public Class frmPatientStatus
    Dim blnUnload As Boolean
    Dim mblnChanged As Boolean
    Dim mblnActivate As Boolean

    Dim mblnCancelDisplay As Boolean
    Dim mUserStatusLVId As Integer
    Dim intLevel As Int16

    Public Sub New(Optional ByVal UserStatusId As Integer = -1)

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        mUserStatusLVId = UserStatusId
    End Sub

    Private Sub frmPatientStatus_Activated(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Activated
        Try
            If mblnActivate = True Then
                mblnActivate = False

                pbRecords.Visible = True
                pbRecords.BringToFront()
                Application.DoEvents()
                pbRecords.Position = 20
                pbRecords.Position = 100
                pbRecords.Visible = False
                grdRecords.Focus()
            End If
            Call btnRefresh_Click(Nothing, Nothing)
        Catch ex As Exception
            pbRecords.Visible = False
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub frmPatientStatus_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
        mblnCancelDisplay = True
        If blnKeyTrap(e.KeyCode, Me, True, grdRecords) = True Then mblnCancelDisplay = False : Exit Sub
        mblnCancelDisplay = False

        pbRecords.Visible = False
    End Sub

    Private Sub frmPatientStatus_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dsTemp As DataSet
        Dim TempControl As Object
        Dim rowCount As Int16 = 0
        Try
            dsTemp = FillDataSet("SELECT PatientStatusId,PatientStatus FROM LtblPatientStatus Order by PatientStatusId Desc")
            For Each TempControl In grdSelectStatus.Controls
                If TempControl.GetType.Name = "SimpleButton" AndAlso TempControl.text <> "Show All" Then
                    TempControl.Text = dsTemp.Tables(0).Rows(rowCount).Item("PatientStatus")
                    TempControl.Tag = dsTemp.Tables(0).Rows(rowCount).Item("PatientStatusId")
                End If
                If TempControl.text <> "Show All" Then rowCount += 1
            Next
            Application.DoEvents()
            Call SetImageControl(Me)
            rgSelectMode.SelectedIndex = -1
            Me.Icon = System.Drawing.Icon.FromHandle(CType(mdiMain.imgFormIcons.Images(2), Bitmap).GetHicon())
            Call GetAccessPermission(Me, "frmPatientStatus")
            Call setPBRecords(Me, pbRecords)
            Call InitializedDataEntry()

            If mUserStatusLVId <> -1 Then
                rgSelectMode.SelectedIndex = 1
            Else
                rgSelectMode.SelectedIndex = 0
                Call btnStatus_Click(btnShowAll, Nothing)
            End If
            mblnActivate = True
            mblnChanged = False

        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub InitializedDataEntry()
        Dim dtTemp As New DataTable("Status Note")

        With dtTemp.Columns
            .Add("Name", GetType(String))
            .Add("Visit Type", GetType(String))
            .Add("Arrival Time", GetType(Date))
            If rgSelectMode.SelectedIndex = 0 Then  'Change status
                .Add("Patient Status", GetType(String))
            End If
            .Add("VisitId", GetType(Integer))
            .Add("PatientId", GetType(Integer))
            .Add("PracticeId", GetType(Integer))

        End With
        gdvRecords.Columns.Clear()
        grdRecords.DataSource = dtTemp

        With gdvRecords
            .Columns("VisitId").Visible = False
            .Columns("VisitId").OptionsColumn.ShowInCustomizationForm = False
            .Columns("PatientId").Visible = False
            .Columns("PatientId").OptionsColumn.ShowInCustomizationForm = False
            .Columns("PracticeId").Visible = False
            .Columns("PracticeId").OptionsColumn.ShowInCustomizationForm = False
            .OptionsView.ColumnAutoWidth = False
            If rgSelectMode.SelectedIndex = 0 Then  'Change status
                .Columns("Name").Width = 255
                .Columns("Visit Type").Width = 70
                .Columns("Arrival Time").Width = 80
                .Columns("Patient Status").Width = 90
            Else
                .Columns("Name").Width = 315
                .Columns("Visit Type").Width = 90
                .Columns("Arrival Time").Width = 90
            End If


        End With

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub rgSelectMode_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles rgSelectMode.SelectedIndexChanged
        Call InitializedDataEntry()
        If rgSelectMode.SelectedIndex = 0 Then  'Change status
            For Each TempControl As Object In grdSelectStatus.Controls
                If TempControl.GetType.Name = "SimpleButton" Then
                    TempControl.Enabled = True
                End If
            Next
            Call btnStatus_Click(btnShowAll, Nothing)
        ElseIf rgSelectMode.SelectedIndex = 1 Then  'Open notes

            If mUserStatusLVId = -1 Then
                intLevel = IIf(gintUserLevelId = 1 OrElse gintUserLevelId = 2, PatientStatus.AwaitingDoctor, PatientStatus.AwaitingNurse)
            Else
                intLevel = mUserStatusLVId
            End If
            For Each TempControl As Object In grdSelectStatus.Controls
                If TempControl.GetType.Name = "SimpleButton" Then
                    If TempControl.Tag = intLevel Then
                        Call btnStatus_Click(TempControl, e)
                    Else
                        TempControl.Enabled = False
                    End If
                End If
            Next
        End If
    End Sub

    Private Sub btnStatus_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
    btnAwaitingNurse.Click, btnWithNurse.Click, btnAwaitingDoctor.Click, btnWithDoctor.Click, _
    btnSeenByDoctor.Click, btnVisitComplete.Click, btnLeftToReturn.Click, btnShowAll.Click
        Dim dsTemp As DataSet
        Dim strDoctorFilter As String = ""
        Dim strQuery As String

        Try
            If gintUserLevelId = 1 OrElse gintUserLevelId = 2 Then
                strDoctorFilter = strRecordID("tblPracticeInfo", "PracticeId", " UserId=" & gintUserID)
                If strDoctorFilter = "Null" Then
                    strDoctorFilter = ""
                Else
                    strDoctorFilter = " AND PracticeId=" & CInt(strDoctorFilter)
                End If
            End If
            If rgSelectMode.SelectedIndex = 0 Then  'Change status
                strQuery = "SELECT DISTINCT tblPatientInfo.PatientTitle + ' ' + (IsNull(tblPatientInfo.LastName + ', ' , '') + tblPatientInfo.FirstName + Isnull(' ' + tblPatientInfo.MiddleName, '') ) [Name],AppointmentType [Visit Type]," & _
                      "CAST(DATEPART(hh,VisitDate) as char(2)) + ':' + CAST(DATEPART(n,VisitDate) as char(2)) [Arrival Time]," & _
                      "PatientStatus [Patient Status] ," & _
                      "VisitId,tblPatientInfo.PatientId [PatientId],PracticeId FROM (tblPatientInfo  INNER  JOIN " & _
                      "(tblVisits JOIN LtblAppointmentType ON tblVisits.AppointmentTypeId=LtblAppointmentType.AppointmentTypeId) ON  " & _
                      "tblPatientInfo.PatientId = tblVisits.PatientId ) JOIN LtblPatientStatus ON LtblPatientStatus.PatientStatusId =tblVisits.PatientStatusId" & _
                      " WHERE VisitDate BETWEEN '" & CDate(Today).ToShortDateString & " 00:00' and '" & CDate(Today).ToShortDateString & " 23:59' " & _
                      IIf(sender.tag Is Nothing, "", " AND LtblPatientStatus.PatientStatusId =" & CInt(sender.Tag)) & strDoctorFilter

            Else
                strQuery = "SELECT DISTINCT tblPatientInfo.PatientTitle + ' ' + (IsNull(tblPatientInfo.LastName + ', ' , '') + tblPatientInfo.FirstName + Isnull(' ' + tblPatientInfo.MiddleName, '') ) [Name],AppointmentType [Visit Type]," & _
                      "CAST(DATEPART(hh,VisitDate) as char(2)) + ':' + CAST(DATEPART(n,VisitDate) as char(2)) [Arrival Time]," & _
                      "VisitId,tblPatientInfo.PatientId [PatientId],PracticeId FROM (tblPatientInfo  INNER  JOIN " & _
                      "(tblVisits JOIN LtblAppointmentType ON tblVisits.AppointmentTypeId=LtblAppointmentType.AppointmentTypeId) ON  " & _
                      "tblPatientInfo.PatientId = tblVisits.PatientId ) WHERE PatientStatusId =" & CInt(sender.Tag) & strDoctorFilter & _
                      " AND VisitDate BETWEEN '" & CDate(Today).ToShortDateString & " 00:00' and '" & CDate(Today).ToShortDateString & " 23:59' "

            End If

            dsTemp = FillDataSet(strQuery)

            With grdRecords
                .BeginUpdate()
                .DataSource = dsTemp.Tables(0)
                .EndUpdate()
                .ForceInitialize()
            End With
            Dim TempDate As DateTime
            For i As Int16 = 0 To gdvRecords.RowCount - 1
                TempDate = gdvRecords.GetRowCellValue(i, "Arrival Time")
                gdvRecords.SetRowCellValue(i, "Arrival Time", TempDate.ToShortTimeString)
            Next
            If sender.Text = "Show All" Then
                grdStatusInformation.Text = "All"
            Else
                grdStatusInformation.Text = sender.Text
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub

    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        For Each TempControl As Object In grdSelectStatus.Controls
            If grdStatusInformation.Text = "All" Then
                Call btnStatus_Click(btnShowAll, e)
                Exit For
            Else
                If TempControl.GetType.Name = "SimpleButton" AndAlso TempControl.text = grdStatusInformation.Text Then
                    Call btnStatus_Click(TempControl, e)
                    Exit For
                End If
            End If
        Next
    End Sub

    Private Sub grdRecords_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles grdRecords.DoubleClick
        Dim frmTempForm As Form = Nothing

        Try
            LoadCursor(Me)
            If rgSelectMode.SelectedIndex = 0 Then  'Change status
                frmTempForm = New frmRegistration(gdvRecords.GetFocusedRowCellValue("VisitId"))
                frmTempForm.ShowInTaskbar = False
                frmTempForm.ShowDialog()
                Call btnRefresh_Click(Nothing, Nothing)
            ElseIf rgSelectMode.SelectedIndex = 1 Then  'Open notes
                If intLevel = PatientStatus.AwaitingDoctor Then
                    'frmTempForm = New frmDoctorsNote(gdvRecords.GetFocusedRowCellValue("VisitId"))
                    Dim frmTempDoctorsNote As New frmDoctorsNote
                    With frmTempDoctorsNote
                        .MdiParent = Me.ParentForm
                        .VisitId = gdvRecords.GetFocusedRowCellValue("VisitId")
                        .Show()
                    End With
                ElseIf intLevel = PatientStatus.AwaitingNurse Then
                    frmTempForm = New frmNurseNote(gdvRecords.GetFocusedRowCellValue("VisitId"))

                ElseIf mUserStatusLVId = PatientStatus.SeenByDoctor Then

                    frmTempForm = New frmChargeAndPayment(gdvRecords.GetFocusedRowCellValue("VisitId"), gdvRecords.GetFocusedRowCellValue("PatientId"), gdvRecords.GetFocusedRowCellValue("PracticeId"))
                    frmTempForm.Text = gdvRecords.GetFocusedRowCellValue("Name") & " (" & Now.ToLongDateString & " )"
                End If
                If IsDBNullOrNothing(frmTempForm) = False Then
                    With frmTempForm
                        .MdiParent = Me.ParentForm
                        .Show()
                    End With
                End If
            End If
           
            DestroyCrushor(Me)
        Catch ex As Exception
            DestroyCrushor(Me)
            MessageBox.Show(ex.Message, gstrMessage, MessageBoxButtons.OK, MessageBoxIcon.Error)
            AddErrorLog("Error " & Err.Number & " (" & Err.Description & ") in Procedure " & _
                Reflection.MethodBase.GetCurrentMethod.Name & " Of " & Me.Name)
        End Try
    End Sub
End Class
